#---Python3.2 代码节选. 不兼容 Python3.3   -----------------------------------

Port = 86 
IpStr="IP=052.033.051.196:00086X"   # 这是和节点直接通讯的IP地址，注意Port是五位数字后面加字符X

#-----辅助函数----------------------------------
import datetime
import sys 

import time
import datetime
import pytz
from pytz import timezone

def hexNibble(nibble):
    #Convert a numeric nibble 0x0-0xF to its ASCII string representation "0"-"F"'''
    hexStr = "0123456789ABCDEF"
    return hexStr[nibble & 0xF]
    

CRC7_POLY = 0x91;
 
def CRCcal(message, length):
  #unsigned char i, j, crc = 0;
  crc=0;  
  #for (i = 0; i < length; i++)
  i=0;
  while(i < length):
    crc ^= ord(message[i]);
    #for (j = 0; j < 8; j++)
    j=0;
    while(j<8):
      if (crc & 1):
        crc ^= CRC7_POLY;
      crc >>= 1;
      j+=1;
    i+=1;
  return crc;
      
#---------------------------------------
                   
#主程序部分代码:按行解析


         if(line[0:5]=="XST=?"): # TimeSync request 
                bj_tz = timezone('Asia/Shanghai')
                bj_time = datetime.datetime.now(bj_tz)
                atime=""

                msg = "ST="+bj_time.strftime ( "%y%m%d_%H%M%S" )
                msg +="X"
                crc= CRCcal(msg,len(msg))
                cs = hexNibble(crc>>4)+hexNibble(crc)
                atime +=msg+cs
                atime +=","

                if(1):
                    msg = IpStr
                    crc= CRCcal(msg,len(msg))
                    cs = hexNibble(crc>>4)+hexNibble(crc)
                    atime +=msg+cs

                atime +="\n"

                log("-T-: "+line)
                log("-T-: "+atime)
                try:
                    sock.sendall(atime)   # 发送此字符串给远程节点
                except Exception as e:
                    log("-E-Socket send: [%d]: %s" % (e.args[0], e.args[1]) )
                    break
--------------------------
